package com.sbc.idempotent.toolkit;

import com.sbc.idempotent.annotation.Idempotent;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.reflect.MethodSignature;

import java.lang.reflect.Method;

/**
 * @description: 切面工具类
 * @author: songbaicheng
 * @create: 2024-05-09 16:28
 **/
public class AspectUtil {

    /**
     * 获取方法上的幂等注解 {@link Idempotent}
     *
     * @param joinPoint 连接点
     * @return 幂等注解事例
     * @throws NoSuchMethodException 无法根据joinPoint找到对应的目标方法时抛出
     */
    public static Idempotent getIdempotent(ProceedingJoinPoint joinPoint) throws NoSuchMethodException {
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        Method targetMethod = joinPoint.getTarget().getClass().getDeclaredMethod(methodSignature.getName(), methodSignature.getMethod().getParameterTypes());
        return targetMethod.getAnnotation(Idempotent.class);
    }
}
